package com.zoewin.zephyr.reportjasper.factory.creator;

import com.zoewin.zephyr.reportjasper.config.JasperConfig;
import com.zoewin.zephyr.reportjasper.entity.ReportEntity;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.IOException;

/**
 * Created by WQL on 2020/8/5
 * CEASE TO STRUGGLE AND YOU CEASE TO LIVE
 */
public class PoiReportCreator {

    public static ReportEntity createPoiReportByWord(JasperConfig config) throws Exception {

        try {
            XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(config.getWordPath()));
            ReportEntity reportEntity = new ReportEntity(document, config.getPicPath());
            return reportEntity;
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("loadWordTemplate: "+config.getJrxmlPath()+" failed, reason: "+e.toString());
        }
    }

    public static ReportEntity createPoiReportByInputStream(JasperConfig config) throws Exception {
        try {
            XWPFDocument document = new XWPFDocument(config.getInputStream());
            ReportEntity reportEntity = new ReportEntity(document, config.getPicPath());
            return reportEntity;
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("loadWordTemplate: "+config.getJrxmlPath()+" failed, reason: "+e.toString());
        }
    }
}
